package com.example.cs_jump.Util;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;
import android.widget.Switch;

import com.example.cs_jump.Bean.JsonBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;

public class ReadJsonParseJson {

    public static void readJSONwithString(Context context, int count) {
        //将数据读取从文件中读取出来
        String filename = count + ".json";

        File intFile = new File(context.getApplicationContext().getExternalFilesDir
                (Environment.DIRECTORY_DOCUMENTS) + File.separator + filename);
        FileInputStream fis;

        try {
            fis = new FileInputStream(intFile);
            StringBuffer buffer = new StringBuffer();
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String data = null;
            while ((data = br.readLine()) != null) {
                buffer.append(data.trim());
            }
//            Log.d("msg", buffer.toString());
            parseJSONWithGSon(buffer.toString(), context, count);
            br.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //将读取出来的json转换成JsonBean对象 并且存入Sqlite数据库中
    public static void parseJSONWithGSon(String jsonData, Context context, int flag) {
        Sqlite sqlite = new Sqlite(context.getApplicationContext());
        SQLiteDatabase db = sqlite.getInstance(context).getWritableDatabase();
        String tablename = "";
        ContentValues values = new ContentValues();
        switch (flag) {
            case 0:
                tablename = "nuke";
                break;
            case 1:
                tablename = "dust";
                break;
            case 2:
                tablename = "cache";
                break;
            case 3:
                tablename = "inferno";
                break;
            case 4:
                tablename = "mirage";
                break;
            case 5:
                tablename = "overpass";
                break;
            case 6:
                tablename = "train";
                break;
            case 7:
                tablename = "vertigo";
                break;

        }
        Gson gson = new Gson();
        List<JsonBean> jsonList = gson.fromJson(jsonData, new TypeToken<List<JsonBean>>() {
        }.getType());
        try {
            for (JsonBean jsonBean : jsonList) {
                values.put("title", jsonBean.getTitle());
                values.put("brief", jsonBean.getBrief());
                values.put("tickrate", jsonBean.getTickrate());
                values.put("cover", jsonBean.getCover());
                values.put("id", jsonBean.getId());
                db.insert(tablename, null, values);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
            sqlite.close();
            values.clear();
        }

    }

}


